﻿
Imports System.Xml
Partial Class user
    Inherits System.Web.UI.Page


    Sub sendMessage(ByVal mailFrom As String, ByVal mailTo As String, ByVal mailSubject As String, ByVal mailBody As String)
        ' sendMessage takes the same parameters as MPS.SendMail
        ' response = sendMessage (From, To, Subject, Body)
        Dim mailer = Server.CreateObject("SMTPsvg.Mailer")

        mailer.FromAddress = mailFrom
        mailer.AddRecipient("", mailTo)
        mailer.Subject = mailSubject
        mailer.BodyText = mailBody

        mailer.RemoteHost = "mail.rapidsite.net"

        If mailer.Sendmail Then               ' Send message
            Dim sendMessage = True                ' Email was sent ok, return True
        Else                                  ' Send Failure   
            Dim sendMessage = mailer.Response     ' Return error message
        End If

    End Sub




    Sub SendMail(ByVal mTo As String, ByVal mFrom As String, ByVal mSubject As String, ByVal mBody As String)
        Dim objCdonts = Server.CreateObject("CDONTS.NewMail")
        objCdonts.To = mTo
        objCdonts.From = mFrom
        objCdonts.Subject = mSubject
        objCdonts.Body = mBody
        objCdonts.Send()
        objCdonts = Nothing
       
    End Sub

    Dim error1 As String = "Lietotja nosaukums pārsniedz maksimālo garumu"
    Dim error2 As String = "Lietotja nosaukums satur neatļautus simbolus"
    Dim error3 As String = "Lietotja nosaukums nav ievadīts"
    Dim error4 As String = "Lietotja bilde nav pieejama"


    Function checkData(ByVal userName As String, ByVal userImage As String) As Boolean


        Dim pattern As String = "^[a-zA-Z0-9]+$"
        Dim reg As New Regex(pattern)
        Dim err As String = ""

        If reg.IsMatch(userName) = False Then err = error2 & "\n"
        If userName.Length > 30 Then err = err & error1 & "\n"
        If userImage = "" Then err = err & error4 & "\n"
        If userName = "" Then err = err & error3 & "\n"

        If err = "" Then Return True

        Response.Write(err)
        Return False

    End Function

    Dim eMessage1 As String = "Neizdevās pievienot lietotāja informāciju"
    Dim eMessage2 As String = "Neizdevās izmainīt lietotāja informāciju"
    Dim eMessage3 As String = "Neizdevās dzēst lietotāja informāciju"
    Dim eMessage4 As String = "Neizdevās saglabāt rezultātus XML datnē"
    Dim eMessage5 As String = "Neizdevās atvērt XML datni rediģēšanai"

    '-----------------------------------------------------------
    'Sistēmas īsinājumikonu saraksta pārvaldības funkcijas class
    'strXMLFile - The path and file name of the XML file.
    'strXSLFilee - The path and file name of the XSL file.
    '----------------------------------------------------------- 

    Sub userXmlAction()

        'Sistēmas lietotaju saraksta pārvaldības funkcijas ievaddati

        Dim userName As String = Request.QueryString("login")
        Dim userFile As String = Request.QueryString("password")

        Dim userOrder As String = Request.QueryString("order")
        Dim userImage As String = Request.QueryString("image")

        Dim userAction As String = Request.QueryString("action")
        Dim userAccess As String = Request.QueryString("access")

        If checkData(userName, userImage) = True Then

            If userAction <> "" Then

                Dim xmlDoc As New XmlDocument()

                Try 'atvērt XML datni rediģēšanai
                    xmlDoc.Load(Server.MapPath("xml_users.xml"))
                    Dim node As XmlNode = xmlDoc.DocumentElement


                    'Sistēmas lietotaju pievienošana
                    If userAction = "add" Then

                        Dim item As XmlElement = xmlDoc.CreateElement("item")

                        item.SetAttribute("name", userName)
                        item.SetAttribute("file", userFile)

                        item.SetAttribute("image", userImage)
                        item.SetAttribute("level", userAccess)

                        Try 'pievienot lietotaja informāciju
                            node.InsertBefore(item, node.ChildNodes(userOrder))
                        Catch exc As Exception

                            Response.Write(eMessage1)
                            Response.Write(exc.ToString)

                        End Try

                    End If

                    'Sistēmas lietotaju dzēšana
                    If userAction = "delete" Then
                        If userOrder <> "" Then

                            Dim item As XmlElement = node.ChildNodes(userOrder)

                            Try 'dzēst lietotaja informāciju
                                node.RemoveChild(item)
                            Catch exc As Exception

                                Response.Write(eMessage3)
                                Response.Write(exc.ToString)

                            End Try

                        End If
                    End If

                    'Sistēmas lietotaja rediģēšana
                    If userAction = "edit" Then
                        If userOrder <> "" Then

                            Dim item As XmlElement = node.ChildNodes(userOrder)

                            item.SetAttribute("login", userName)
                            item.SetAttribute("password", userFile)

                            item.SetAttribute("image", userImage)
                            item.SetAttribute("level", userAccess)

                            Try 'izmainīt lietotja informāciju.
                                node.AppendChild(item)
                            Catch exc As Exception

                                Response.Write(eMessage2)
                                Response.Write(exc.ToString)

                            End Try

                        End If
                    End If


                    Try 'saglabāt rezultātus XML datnē
                        xmlDoc.Save(Server.MapPath("xml_users.xml"))
                    Catch exc As Exception

                        Response.Write(eMessage4)
                        Response.Write(exc.ToString)

                    End Try

                Catch exc As Exception

                    Response.Write(eMessage5)
                    Response.Write(exc.ToString)

                End Try

            End If
        End If

    End Sub


End Class
